﻿using System;

namespace Borg.Framework
{
    internal class PlugControl : IDisposable
    {
        protected Action Action = null;
        protected readonly System.Windows.Forms.Control Control = null;

        public PlugControl(System.Windows.Forms.Control control, Action action)
        {
            Control = control;
            Action = action;

            action.Slot.Registered += EnableControls;
            action.Slot.Deregistered += EnableControls;
        }

        public virtual void Dispose()
        {
            if (Action == null)
                return;

            Action.Slot.Registered -= EnableControls;
            Action.Slot.Deregistered -= EnableControls;

            Action = null;
        }

        private void EnableControls(object sender, RegisterEventArgs args)
        {
            Control.Enabled = Action.Slot.RegisteredPlugTypeInfos.Count > 0;
        }

        public void OnClick(EventArgs e)
        {
            Action.Do(null, null);
        }
    }
}

